package ar.com.ford.it.fullEconomics.view.action;


import java.util.Collection;
import java.util.LinkedList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import ar.com.ford.it.fullEconomics.exception.FullEconomicsException;
import ar.com.ford.it.fullEconomics.facade.CommodityFacade;
import ar.com.ford.it.fullEconomics.facade.FullEconomicsFacade;
import ar.com.ford.it.fullEconomics.struct.FullEconomicsExcercisesStruct;
import ar.com.ford.it.fullEconomics.struct.OptionCombo;
import ar.com.ford.it.fullEconomics.view.form.FullEconomicsPopupForm;
import ar.com.ford.it.fullEconomics.wslx.FullEconomicsWslxUser;
import ar.com.ford.it.webengine.facade.FacadeProvider;
import ar.com.ford.it.webengine.struts.AbstractBaseAction;
import ar.com.ford.it.webengine.struts.FordActionMapping;
import ar.com.ford.it.webengine.websession.WebSession;

public class FullEconomicsPopupAction extends AbstractBaseAction {
    /**
     * 
     * @return Coleccion de posibles estados de Cost Structure
     */
    private Collection getComboStatus(HttpServletRequest request) {
        LinkedList<OptionCombo> list = new LinkedList<OptionCombo>();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);

        list.add(new OptionCombo("Draft", resources.getMessage("label.draft")));
        list.add(new OptionCombo("Proposed", resources
                .getMessage("label.proposed")));
        list.add(new OptionCombo("Approved", resources
                .getMessage("label.approved")));

        return list;
    }

    /**
     * 
     * @param request
     * @return
     */
    private Collection<OptionCombo> getComboSupplierClasses(
                                                            HttpServletRequest request) {
        LinkedList<OptionCombo> list = new LinkedList<OptionCombo>();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);

        list.add(new OptionCombo("F", resources.getMessage("label.ford")));
        list.add(new OptionCombo("C", resources.getMessage("label.custom")));

        return list;
    }

    /**
     * 
     * @param request
     * @return
     */
    private Collection<OptionCombo> getComboPartClasses(
                                                        HttpServletRequest request) {
        LinkedList<OptionCombo> list = new LinkedList<OptionCombo>();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);

        list.add(new OptionCombo("F", resources.getMessage("label.ford")));
        list.add(new OptionCombo("C", resources.getMessage("label.custom")));

        return list;
    }

    /**
     * 
     * @param request
     * @return
     */
    private Collection<OptionCombo> getComboDateTypes(HttpServletRequest request) {
        LinkedList<OptionCombo> list = new LinkedList<OptionCombo>();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);

        list.add(new OptionCombo("Creation", resources
                .getMessage("user.label.creation")));
        list.add(new OptionCombo("Proposal", resources
                .getMessage("user.label.proposal")));
        list.add(new OptionCombo("Approval", resources
                .getMessage("user.label.approval")));
        list.add(new OptionCombo("EffDateIn", resources
                .getMessage("label.effDateIn")));

        return list;
    }

    /**
     * 
     * @param request
     * @return
     */
    private Collection<OptionCombo> getComboUserActionTypes(
                                                            HttpServletRequest request) {
        LinkedList<OptionCombo> list = new LinkedList<OptionCombo>();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);

        list.add(new OptionCombo("Creation", resources
                .getMessage("user.label.creation")));
        list.add(new OptionCombo("Proposal", resources
                .getMessage("user.label.proposal")));
        list.add(new OptionCombo("Approval", resources
                .getMessage("user.label.approval")));

        return list;
    }

    /**
     * Inicializa la pagina de Full Economics Excersices
     */
    public ActionForward initPage(FordActionMapping mapping,
                                  ActionForm actionForm,
                                  HttpServletRequest request,
                                  HttpServletResponse response,
                                  WebSession webSession, ActionMessages msgs,
                                  ActionMessages warns, ActionMessages errorMsgs) {
        FullEconomicsPopupForm form = (FullEconomicsPopupForm) actionForm;
        CommodityFacade commodityFacade = (CommodityFacade) FacadeProvider
                .getFacade(request, CommodityFacade.class);
        FullEconomicsWslxUser user = (FullEconomicsWslxUser) webSession
                .getUser();

        try {
            form.resetForm();
            request.getSession().setAttribute("statusCol",
                    this.getComboStatus(request));
            request.getSession().setAttribute("supplierTypeCol",
                    this.getComboSupplierClasses(request));
            request.getSession().setAttribute("partClassesCol",
                    this.getComboPartClasses(request));
            request.getSession().setAttribute(
                    "commoditiesCol",
                    commodityFacade.findCommodities("", "", user
                            .getTeesg01Plant()));
            request.getSession().setAttribute("userActionsTypesCol",
                    this.getComboUserActionTypes(request));
            request.getSession().setAttribute("dateTypesCol",
                    this.getComboDateTypes(request));
            form.setFullEconomicCodeid(request.getParameter("fullEconomicCodeId"));
        } catch (FullEconomicsException e) {
            ActionMessages am = new ActionMessages();
            am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e
                    .getMessageResource()));
            saveErrors(request, am);
        }
        return mapping.findForward("success");
    }

    /**
     * Genera la busqueda de los full Economics que cumplan con los parametros
     * seleccionados
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward findFullEconomics(FordActionMapping mapping,
                                           ActionForm actionForm,
                                           HttpServletRequest request,
                                           HttpServletResponse response,
                                           WebSession webSession,
                                           ActionMessages msgs,
                                           ActionMessages warns,
                                           ActionMessages errorMsgs) {

        FullEconomicsPopupForm form = (FullEconomicsPopupForm) actionForm;
        FullEconomicsWslxUser user = (FullEconomicsWslxUser) webSession
                .getUser();
        FullEconomicsFacade facade = (FullEconomicsFacade) FacadeProvider
                .getFacade(request, FullEconomicsFacade.class);
        FullEconomicsExcercisesStruct parameters = form.getFindParameters(user);

        try {
            form.setFullEconomics(facade.getFullEconomicsCol(parameters));
        } catch (FullEconomicsException e) {
            ActionMessages am = new ActionMessages();
            am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e
                    .getMessageResource()));
            saveErrors(request, am);
        }
        return mapping.findForward("success");
    }

}
